﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

namespace UnityEngine.UI
{
    [DisallowMultipleComponent]
    [RequireComponent(typeof(UIDynamic))]
    public class UIDynamicAnim : MonoBehaviour
    {

        public UIDynamic UIDynamicComp;

        private System.Action _callback;

        private void Awake()
        {
            Init();
        }

        public void Init()
        {
            if (UIDynamicComp == null)
                UIDynamicComp = GetComponent<UIDynamic>();
        }

        public void OnDisable()
        {
            CancelInvoke("HandleCallback");
        }

        public void HandleCallback()
        {
            if (_callback != null)
            {
                _callback();
            }
        }

        /// <summary>
        /// 让指定下标可见
        /// </summary>
        /// <param name="index"></param>
        /// <param name="duration">动画时间</param>
        public void SetIndexInSightSmoothly(int index, float duration, System.Action callback)
        {
            _callback = callback;
            CancelInvoke("HandleCallback");
            Invoke("HandleCallback", duration);

            int curLine = UIDynamicComp.GetCurScrollPerLineIndex();
            int wantLine = Mathf.CeilToInt(index / UIDynamicComp.constraintCount);

            float cellRect = UIDynamicComp.cellSize.y + UIDynamicComp.spacing.y;
            float scrollRectLength = UIDynamicComp.scrollRectTrans.sizeDelta.y;

            if (UIDynamicComp.scrollAxis == UIDynamic.Arrangement.Horizontal)
            {
                cellRect = UIDynamicComp.cellSize.x + UIDynamicComp.cellSize.x;
                scrollRectLength = UIDynamicComp.scrollRectTrans.sizeDelta.x;
            }

            int lineSight = Mathf.FloorToInt(scrollRectLength / (cellRect));
            int sightRangeMin = curLine + 1;
            int sightRangeMax = curLine + lineSight;

            if (wantLine >= sightRangeMin && wantLine <= sightRangeMax)
            {
                //if (callback != null) callback();
                return;
            }
            Tweener tweener = null;
            //未测试过横向定位是否可以
            if (UIDynamicComp.scrollAxis == UIDynamic.Arrangement.Horizontal)
            {
                if (wantLine < sightRangeMin)
                {
                    float v = (wantLine * cellRect) / UIDynamicComp.MaxAnchorPos;
                    tweener = UIDynamicComp.scrollRect.DOHorizontalNormalizedPos(1 - v, duration);
                }
                else if (wantLine > sightRangeMax)
                {
                    float v = ((wantLine + 1) * cellRect - scrollRectLength) / UIDynamicComp.MaxAnchorPos;
                    tweener = UIDynamicComp.scrollRect.DOHorizontalNormalizedPos(1 - v, duration);
                }
            }
            else
            {
                if (wantLine < sightRangeMin)
                {
                    float v = (wantLine * cellRect) / UIDynamicComp.MaxAnchorPos;
                    //scrollRect.verticalNormalizedPosition = 1 - v;
                    tweener = UIDynamicComp.scrollRect.DOVerticalNormalizedPos(1 - v, duration);
                }
                else if (wantLine > sightRangeMax)
                {
                    float v = ((wantLine + 1) * cellRect - scrollRectLength) / UIDynamicComp.MaxAnchorPos;
                    //scrollRect.verticalNormalizedPosition = 1 - v;
                    tweener = UIDynamicComp.scrollRect.DOVerticalNormalizedPos(1 - v, duration);
                }
            }
            //if (tweener != null)
            //{
            //    tweener.OnComplete(() => { if (callback != null) callback(); });
            //}
        }

        public void TweenItemPos(Transform itemTrans, Vector3 position,float duration = 0.3f)
        {
            itemTrans.DOLocalMove(position, duration).SetEase(Ease.Linear);
        }

        public void KillTweener(object item) { DOTween.Kill(item); }

        public void DoSizeDelta(RectTransform rt, Vector2 size, float duration = 0.3f)
        {
            rt.DOSizeDelta(size,duration).SetEase(Ease.Linear);
        }

        public void DoAnchorPos(RectTransform rt, Vector2 pos, float duration = 0.3f,System.Action onComplete = null)
        {
            rt.DOAnchorPos(pos, duration).SetEase(Ease.Linear).OnComplete(()=> { onComplete?.Invoke(); });
        }

        public void FadeIn(UIDynamicItem item)
        {
            CanvasGroup cg = item.GetCanvaGroup();
            cg.alpha = 0;
            cg.DOFade(1, 0.2f).SetEase(Ease.Linear);
        }

        public void DisableVertival(float duration = 0.3f)
        {
            UIDynamicComp.scrollRect.vertical = false;
            CancelInvoke("EnableVertival");
            Invoke("EnableVertival", duration);
        }
        public void EnableVertival()
        {
            UIDynamicComp.scrollRect.vertical = true;
        }
    }
}